package com.demo.controller;

import com.demo.entity.Operator;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.*;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * Created by lmn on 2018/5/22.
 */
public class LambdaController {
    public static void main(String[] args) {
        Supplier<String> stringSupplier = () -> new String ();
        String str = stringSupplier.get();

        List<Integer> list = Arrays.asList(1,5,3,6,9);
        List<String> strs = map(list,String ::new);

        List<String> names = Arrays.asList("name","age");
        List<Integer> lengths = names.stream().map(String::length).collect(toList());

        String hello = "hello";
        Arrays.stream(hello.split("")).forEach(s -> System.out.println(s));


        String[] cities = {"北京","上海","广州","深圳","上海","广州","深圳","广州","深圳","广州","深圳"};
        String[] homes = {"剑桥","上海","广州","深圳","上海","广州","深圳","广州","深圳","广州","深圳"};
        List<Operator> operators = new ArrayList();
        for (int i = 0; i < 10; i++) {
            Operator operator = new Operator();
            operator.setName("name"+i);
            operator.setHome(homes[i]);
            operator.setYear(i+"");
            operator.setCity(cities[i]);
            operator.setPrice(new BigDecimal(i*10000));
            operators.add(operator);
        }

        //找出2011年发生的所有交易，并按交易额排序
        List<Operator> allMatchs = operators.stream().filter(o -> "2011".equals(o.getYear())).sorted(Comparator.comparing(Operator::getPrice)).collect(toList());
        //交易员都在哪些不同的城市工作过
        List<String> citiesOpers = operators.stream().map(Operator::getCity).distinct().collect(toList());
        Set<String> citieSet = operators.stream().map(Operator::getCity).collect(toSet());
        //查找所有来自于剑桥的交易员，并按姓名排序
        List<Operator> jianqiaos = operators.stream().filter(operator -> "剑桥".equals(operator.getHome())).distinct().sorted(Comparator.comparing(Operator::getName)).collect(toList());
        //返回所有交易员的姓名字符串，按字母顺序排序 🌟🌟🌟🌟🌟🌟
        String nameStr = operators.stream().map(operator -> operator.getName()).distinct().sorted().reduce("",(n1,n2) -> n1+n2);
        //有没有交易员是在米兰工作的
        Boolean milan = operators.stream().anyMatch(operator -> "米兰".equals(operator.getCity()));
        //打印生活在剑桥的交易员的所有交易额
        BigDecimal total = operators.stream().map(operator -> operator.getPrice()).reduce(new BigDecimal(0),BigDecimal::add);

        List<BigDecimal> jian = operators.stream().filter(operator -> "剑桥".equals(operator.getHome())).map(Operator::getPrice).collect(toList());
        jian.stream().forEach(bigDecimal -> System.out.println(bigDecimal));
        //所有交易中，最高的交易额是多少
        Optional<Operator> optional = operators.stream().max(Comparator.comparing(Operator::getPrice));
        Optional<BigDecimal> maxPrice= operators.stream().map(Operator::getPrice).reduce(BigDecimal::max);
        //找到交易额最小的交易
        Optional<Operator> minOper = operators.stream().min(Comparator.comparing(Operator::getPrice));


        //生成流  Stream.of/iterate
        //Stream.of 生成流
        Stream.of("Spring","in","action").map(String::toUpperCase).forEach(System.out::println);

        Stream.iterate(0,n -> n+2).limit(10).forEach(System.out::println);
        //斐波纳契元组序列
        Stream.iterate(new int[]{0,1},n -> new int[]{n[1],n[0]+n[1]}).limit(20).map(n->n[0]).forEach(System.out::println);

//        Stream.generate(Math::random).limit(5).forEach(System.out::println);

    }

    private static List<String> map(List<Integer> list,Function<String,String> objects){
        List<String> list1 = new ArrayList<>();
        for (Integer i:list){
            list1.add(objects.apply(i.toString()));
        }
        return list1;
    }
}
